home *** CD-ROM | disk | FTP | other *** search
- /*
- * eXtended-Library
- * released for GNU-C++
- * by Volker Hemsen, 05/96
- * Public Domain / Freeware
- *
- * Pascal-like 'set of char'
- * but uses 256 chars !!!
- * Operators: = == != + += - -= & &&(=Pascal-in)
- * Operands: SET_OF_CHAR char char*
- */
-
- #ifdef __cplusplus
- #ifndef _SETOFCHAR_H
- #define _SETOFCHAR_H
-
- #include <bool.h>
- #include <string.h>
- #include <stdio.h>
-
- class SET_OF_CHAR
- {
- private:
- long bits[8]; /* Bitvektor */
- void clr(char c) /* Bit c löschen */
- {
- bits[c/32]&=~(1L<<(c%32));
- }
- void clrs(char *cs) /* Bits cs[0].. löschen */
- {
- while (*cs)
- clr(*cs++);
- }
- void clrall(void) /* alle Bits loeschen */
- {
- register int ii;
- for(ii=0;ii<=7;bits[ii++]=0L);
- }
- void set(char c) /* Bit c setzen */
- {
- bits[c/32]|=(1L<<(c%32));
- }
- void sets(char *cs) /* Bits cs[0].. löschen */
- {
- while (*cs)
- set(*cs++);
- }
- bool get(char c) /* Bit c prüfen */
- {
- return (bits[c/32]&(1L<<(c%32)));
- }
- void clrbits(long *l) /* alle in l gesetzen Bits im Vektor löschen */
- {
- register int ii;
- for(ii=0;ii<=7;ii++)
- bits[ii]&=~(*l++);
- }
- void setbits(long *l) /* alle in l gesetzen Bits im Vektor setzen */
- {
- register int ii;
- for(ii=0;ii<=7;ii++)
- bits[ii]|=(*l++);
- }
- bool cmpbits(long *l) /* Inhalt von Vektor l mit bits vergleichen */
- {
- register int ii;
- for(ii=0;ii<=7;ii++)
- if (bits[ii]!=*l++)
- return FALSE;
- return TRUE;
- }
- void andbits(long *l) /* Inhalt des Bitvektors mit l undieren */
- {
- register int ii;
- for(ii=0;ii<=7;ii++)
- bits[ii]&=*l++;
- }
- void cpyfrombits(long *to) /* Inhalt von bits nach to kopieren */
- {
- memcpy(to,bits,32);
- }
- void cpytobits(long *from) /* Inhalt von from nach bits kopieren */
- {
- memcpy(bits,from,32);
- }
- public:
- /* Konstruktoren */
- inline SET_OF_CHAR() { clrall(); };
- inline SET_OF_CHAR(SET_OF_CHAR& s) { cpytobits(s.bits); }
- SET_OF_CHAR(char c) { clrall(); set(c); }
- SET_OF_CHAR(char *cs) { clrall(); sets(cs); }
- /* Operator = : Zuweisungen */
- void operator = (char c) { clrall(); set(c); }
- inline void operator = (SET_OF_CHAR s) { cpytobits(s.bits); }
- void operator = (char *cs) { clrall(); sets(cs); }
- /* Operator + : Verknüpfungen */
- SET_OF_CHAR operator + (SET_OF_CHAR a) {
- SET_OF_CHAR s=a;
- register int ii;
- for(ii=0;ii<=7;s.bits[ii]|=bits[ii],ii++);
- return s;
- }
- SET_OF_CHAR operator + (char c) {
- SET_OF_CHAR s;
- cpyfrombits(s.bits);
- s.set(c);
- return s;
- }
- SET_OF_CHAR operator + (char *cs) {
- SET_OF_CHAR s;
- cpyfrombits(s.bits);
- s.sets(cs);
- return s;
- }
- /* Operator += : Verknüpfen und Zuweisen */
- inline void operator += (char c) { set(c); }
- inline void operator += (char *cs) { sets(cs); }
- inline void operator += (SET_OF_CHAR a) { setbits(a.bits); }
- /* Operator - : Ausgrenzen */
- SET_OF_CHAR operator - (char c) {
- SET_OF_CHAR s;
- cpyfrombits(s.bits);
- s.clr(c);
- return s;
- }
- SET_OF_CHAR operator - (char *cs) {
- SET_OF_CHAR s;
- cpyfrombits(s.bits);
- s.clrs(cs);
- return s;
- }
- SET_OF_CHAR operator - (SET_OF_CHAR a) {
- SET_OF_CHAR s;
- cpyfrombits(s.bits);
- s.clrbits(a.bits);
- return s;
- }
- /* Operator -= : Ausgrenzen und Zuweisen */
- inline void operator -= (char c) { clr(c); }
- inline void operator -= (char *cs) { clrs(cs); }
- inline void operator -= (SET_OF_CHAR a) { clrbits(a.bits); }
- /* Operator & : Schnittmenge bilden */
- SET_OF_CHAR operator & (char c) {
- SET_OF_CHAR s=c;
- s.andbits(bits);
- return s;
- }
- SET_OF_CHAR operator & (char *cs) {
- SET_OF_CHAR s=cs;
- s.andbits(bits);
- return s;
- }
- SET_OF_CHAR operator & (SET_OF_CHAR a) {
- SET_OF_CHAR s=a;
- s.andbits(bits);
- return s;
- }
- /* Operator && : auf Bestandteil prüfen */
- inline bool operator && (char c) { return (get(c)!=0); }
- bool operator && (char *cs) {
- while (*cs)
- if (!get(*cs++))
- return FALSE;
- return TRUE;
- }
- bool operator && (SET_OF_CHAR a) {
- register int ii;
- for(ii=0;ii<=255;ii++) {
- if ((a.get((char)ii)) && (!get((char)ii)))
- return FALSE;
- }
- return TRUE;
- }
- /* Operator == : kompletten Inhalt vergleichen */
- bool operator == (char c) {
- SET_OF_CHAR s=c;
- return cmpbits(s.bits);
- }
- bool operator == (char *cs) {
- SET_OF_CHAR s=cs;
- return cmpbits(s.bits);
- }
- inline bool operator == (SET_OF_CHAR a) { return cmpbits(a.bits); }
- /* Operator != : kompletten Inhalt vergleichen, negative Reaktion */
- bool operator != (char c) {
- SET_OF_CHAR s=c;
- return !cmpbits(s.bits);
- }
- bool operator != (char *cs) {
- SET_OF_CHAR s=cs;
- return !cmpbits(s.bits);
- }
- inline bool operator != (SET_OF_CHAR a) { return !cmpbits(a.bits); }
- /* internen Bitvektor zurückgeben */
- inline long *bitptr(void) { return bits; }
- /* set in einen String zurückwandeln */
- char *out(char *cs) {
- register char *cp=cs;
- register ii;
- for(ii=1;ii<=255;ii++) {
- if (get((char)ii))
- *cp++=(char)ii;
- }
- *cp='\0';
- return cs;
- }
- /* set in einen String wandeln und auf Kanal std ausgeben, next anhängen */
- void cout(FILE *std,char *next) {
- char s[256];
- if (std==NULL)
- std=stdout;
- fprintf(std,"%s",out(s));
- if ((next!=NULL) && (*next!=0))
- fprintf(std,"%s",next);
- }
- };
-
- #endif /* #ifndef _SETOFCHAR_H */
- #endif /* #ifdef __cplusplus */
-